using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenMTS.Properties;

namespace OpenMTS
{
    public partial class RunPreferences : Form
    {
    
        public RunPreferences()
        {
            InitializeComponent();
        }
    

        private void btnInputFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files (*.xml)|*.xml|All files (*.*)|*.*";
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtInputMessage.Text = dlg.FileName;
            }
        }
        
        private void btnContinue_Click(object sender, EventArgs e)
        {
            if (txtInputMessage.Text.Length == 0 || txtRuleset.Text.Length == 0)
            {
                MessageBox.Show("Message File and Ruleset file are required.");
                btnContinue.Enabled = false;
                return;
            }

            if (!rdoXml.Checked && txtReport.Text.Length == 0)
            {
                MessageBox.Show("Report Template is required. Please set in Preferences.");
                btnContinue.Enabled = false;
                return;
            }
                       
            Settings.Default.InputFile = this.txtInputMessage.Text;
            Settings.Default.ViewAsXml = this.rdoXml.Checked;
            Settings.Default.Save();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void RunPreferences_Load(object sender, EventArgs e)
        {
            if (Settings.Default.InputFile != null)
                this.txtInputMessage.Text = Settings.Default.InputFile;

            if (Settings.Default.RulesetFile != null)
                this.txtRuleset.Text = Settings.Default.RulesetFile;

            if (Settings.Default.ReportTemplate != null)
                this.txtReport.Text= Settings.Default.ReportTemplate;

            if (Settings.Default.ViewAsXml != null)
            {
                if (Settings.Default.ViewAsXml)
                    this.rdoXml.Checked = true;
                else
                    this.radioButton2.Checked = true;
            }
            else
                this.rdoXml.Checked = true;

            if (this.txtInputMessage.Text.Length == 0 ||
                this.txtRuleset.Text.Length == 0)
                this.btnContinue.Enabled = false;

            if(this.txtRuleset.Text.Length == 0)
               MessageBox.Show("Please set the Ruleset file in File | Preferences before you continue.");
        }

        private void txtInputMessage_TextChanged(object sender, EventArgs e)
        {
            if (txtInputMessage.Text.Length > 0 && txtRuleset.Text.Length > 0)
                btnContinue.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}